package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list", name = "进入菜单管理页面")
    public String fandAll(@RequestParam(defaultValue = "1", name = "page") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Module> page = moduleService.findPage(pageNum, pageSize);
        request.setAttribute("page", page);
        return "system/module/module-list";
    }

    @RequestMapping(value = "/toAdd", name = "进入菜单新增页面")
    public String toAdd(){
        List<Module> list = moduleService.findAll();
        request.setAttribute("menus",list);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/edit", name = "保存菜单方法")
    public String edit(Module module) {
        if (StringUtils.isEmpty(module.getId())) {
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        } else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "进入修改菜单页面")
    public String toUpdate(String id) {
        Module module = moduleService.findById(id);
        request.setAttribute("module", module);
        List<Module> list = moduleService.findAll();
        request.setAttribute("menus",list);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/delete", name = "删除菜单方法")
    public String delete(String id) {
        moduleService.deleteById(id);
        return "redirect:/system/module/list.do";
    }
}
